package com.tt.aile.item.controller;

import com.tt.aile.common.pojo.PageResult;
import com.tt.aile.item.pojo.Sku;
import com.tt.aile.item.pojo.Spu;
import com.tt.aile.item.pojo.SpuDetail;
import com.tt.aile.item.pojo.bo.SpuBo;
import com.tt.aile.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 86176 shkstrat
 * @date 2020/10/6 - 16:36
 */
@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    //分页查询商品
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuBoByPage(
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "saleable", required = false)Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows
    ){

        PageResult<SpuBo> spuBos = this.goodsService.querySpuBoByPage(key, saleable, page, rows);

        if (CollectionUtils.isEmpty(spuBos.getItems())){
            return ResponseEntity.ok(spuBos);
        }
        return ResponseEntity.ok(spuBos);
    }

    //新增商品
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){

        try {
            this.goodsService.saveGoods(spuBo);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    //根据spuId查询spu信息
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){

        SpuDetail spuDetail = this.goodsService.querySpuDetailBySpuId(spuId);
        if (spuDetail!=null){
            return ResponseEntity.ok(spuDetail);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }


    //根据spuId查询sku
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId){

        List<Sku> skus = this.goodsService.querySkuBySpuId(spuId);
        if (CollectionUtils.isEmpty(skus)){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(skus);
    }

    //修改商品上下架
    @PutMapping("spu")
    public ResponseEntity<Void> putGoodsOnOrDownByspuId(
            @RequestParam("id") Long spuId,
            @RequestParam("saleable") boolean saleable){
//        System.out.println("---------------------------"+spuId);
        try {
            this.goodsService.putGoodsOnOrDownByspuId(saleable,spuId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    //更新商品
     @PutMapping("goods")
        public ResponseEntity<SpuBo> updateGoods(@RequestBody SpuBo spuBo){
//         System.out.println("更新商品-------"+spuBo);
         try {
                this.goodsService.updateGoods(spuBo);
                return ResponseEntity.status(HttpStatus.OK).build();
            } catch (Exception e) {
                e.printStackTrace();
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        }
}
